package com.itboye.jigongbao.retrofit.presenter;

import android.app.Activity;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.itboye.jigongbao.app.MyApplication;
import com.itboye.jigongbao.bean.ConfigInfo;
import com.itboye.jigongbao.bean.FinanceBillBean;
import com.itboye.jigongbao.bean.JiYiBiBean;
import com.itboye.jigongbao.bean.LoginInfo;
import com.itboye.jigongbao.bean.MonthAccountBean;
import com.itboye.jigongbao.bean.NotifyMessageBean;
import com.itboye.jigongbao.bean.UpdateImageBean;
import com.itboye.jigongbao.bean.WorkerBean;
import com.itboye.jigongbao.retrofit.base.BaseObserver;
import com.itboye.jigongbao.retrofit.base.BaseResponse;
import com.itboye.jigongbao.retrofit.base.ResultEntity;
import com.itboye.jigongbao.retrofit.util.BuilMapUtils;
import com.itboye.jigongbao.retrofit.util.ResponseListener;
import com.itboye.jigongbao.utils.Const;
import com.itboye.jigongbao.utils.LoadingDialog;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * UserPrestener
 * <p>
 * Created by Mr.w on 2017/12/11.
 * <p>
 * 版本      ${version}
 * <p>
 * 修改时间
 * <p>
 * 修改内容
 */


public class UserPrestener extends Observable {


    public static String By_SecurityCode_send_Success = "_By_SecurityCode_send_Success";
    public static String By_SecurityCode_send_Fail = "_By_SecurityCode_send_Fail";

    public static String By_UserAccount_loginByMobile_Success = "_By_UserAccount_loginByMobile_Success";
    public static String By_UserAccount_loginByMobile_Fail = "_By_UserAccount_loginByMobile_Fail";
    public static String By_UserProfile_update_Success = "_By_UserProfile_update_Success";
    public static String By_UserProfile_update_Fail = "_By_UserProfile_update_Fail";
    public static String avatar_Success = "_avatar_Success";
    public static String avatar_Fail = "_avatar_Fail";
    public static String By_JgbRecord_add_Success = "_By_JgbRecord_add_Success";
    public static String By_JgbRecord_add_Fail = "_By_JgbRecord_add_Fail";
    public static String By_JgbRecord_update_Success = "_By_JgbRecord_update_Success";
    public static String By_JgbRecord_update_Fail = "_By_JgbRecord_update_Fail";
    public static String By_JgbRecord_workerIndex_Success = "_By_JgbRecord_workerIndex_Success";
    public static String By_JgbRecord_workerIndex_Fail = "_By_JgbRecord_workerIndex_Fail";
    public static String By_JgbRecord_queryWorker_Success = "_By_JgbRecord_queryWorker_Success";
    public static String By_JgbRecord_queryWorker_Fail = "_By_JgbRecord_queryWorker_Fail";
    public static String By_JgbRecord_queryAuditRecordForWorker_Success = "_By_JgbRecord_queryAuditRecordForWorker_Success";
    public static String By_JgbRecord_queryAuditRecordForWorker_Fail = "_By_JgbRecord_queryAuditRecordForWorker_Fail";
    public static String By_JgbRecord_audit_Success = "_By_JgbRecord_audit_Success";
    public static String By_JgbRecord_audit_Fail = "_By_JgbRecord_audit_Fail";
    public static String By_JgbRecord_queryOverseer_Success = "_By_JgbRecord_queryOverseer_Success";
    public static String By_JgbRecord_queryOverseer_Fail = "_By_JgbRecord_queryOverseer_Fail";
    public static String By_JgbRecord_queryAuditRecordForOverseer_Success = "_By_JgbRecord_queryAuditRecordForOverseer_Success";
    public static String By_JgbRecord_queryAuditRecordForOverseer_Fail = "_By_JgbRecord_queryAuditRecordForOverseer_Fail";
    public static String By_JgbFinanceBill_worker_Success = "_By_JgbFinanceBill_worker_Success";
    public static String By_JgbFinanceBill_worker_Fail = "_By_JgbFinanceBill_worker_Fail";
    public static String By_JgbFinanceBill_overseer_Success = "By_JgbFinanceBill_overseer_Success";
    public static String By_JgbFinanceBill_overseer_Fail = "_By_JgbFinanceBill_overseer_Fail";
    public static String By_JgbRecord_queryAllWorkerName_Success = "_By_JgbRecord_queryAllWorkerName_Success";
    public static String By_JgbRecord_queryAllWorkerName_Fail = "_By_JgbRecord_queryAllWorkerName_Fail";
    public static String By_JgbRecord_overseerIndex_Success = "_By_JgbRecord_overseerIndex_Success";
    public static String By_JgbRecord_overseerIndex_Fail = "_By_JgbRecord_overseerIndex_Fail";
    public static String By_JgbRecord_info_Success = "_By_JgbRecord_info_Success";
    public static String By_JgbRecord_info_Fail = "_By_JgbRecord_info_Fail";
    public static String By_Message_query_Success = "_By_Message_query_Success";
    public static String By_Message_query_Fail = "_By_Message_query_Fail";
    public static String By_Message_info_Success = "_By_Message_info_Success";
    public static String By_Message_info_Fail = "_By_Message_info_Fail";
    public static String By_Message_unreadCount_Success = "_By_Message_unreadCount_Success";
    public static String By_Message_unreadCount_Fail = "_By_Message_unreadCount_Fail";
    public static String By_Message_del_Success = "_By_Message_del_Success";
    public static String By_Message_del_Fail = "_By_Message_del_Fail";
    public static String By_Suggest_add_Success = "_By_Suggest_add_Success";
    public static String By_Suggest_add_Fail = "_By_Suggest_add_Fail";

    public UserPrestener(Observer observer) {
        this.addObserver(observer);
    }

    public void getConfig(String app_type, Activity activity) {
        String typeKey = "By_Config_app";
        HashMap<String, Object> map = new HashMap<>();
        map.put("app_type", app_type);
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<ConfigInfo>> observable = MyApplication.Companion.getInstance().getMIApi().By_Config_app(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<ConfigInfo>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(ConfigInfo.class));
    }


    public void By_SecurityCode_send(String country, String mobile, int code_type, String send_type, int expire_time, Activity activity) {
        String typeKey = "By_SecurityCode_send";
        HashMap<String, Object> map = new HashMap<>();
        map.put("country", country);
        map.put("mobile", mobile);
        map.put("code_type", code_type);
        map.put("send_type", send_type);
        map.put("expire_time", expire_time);
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "101").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_SecurityCode_send(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_SecurityCode_send_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_SecurityCode_send_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(String.class));
    }

    public void By_UserAccount_loginByMobile(String country_no, String mobile, String code, String password, String role, String device_token, String device_type, Activity activity) {
        String typeKey = "By_UserAccount_loginByMobile";
        HashMap<String, Object> map = new HashMap<>();
        map.put("country_no", country_no);
        map.put("mobile", mobile);
        map.put("code", code);
        if (password != null) {
            map.put("password", password);
        }
        if (role != null) {
            map.put("role", role);
        }
        map.put("device_token", device_token);
        map.put("device_type", device_type);
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "103").buildToString();
        rx.Observable<BaseResponse<LoginInfo>> observable = MyApplication.Companion.getInstance().getMIApi().By_UserAccount_loginByMobile(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<LoginInfo>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_UserAccount_loginByMobile_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_UserAccount_loginByMobile_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(LoginInfo.class));
    }

    /**
     * @param uid              用户id
     * @param nickname         昵称(中文 + 英文字母)
     * @param sign             个人签名(最大256个字符,一个中文计一个字符)
     * @param sex              性别（0: 女 1: 男 2: 双或未知)
     * @param realname         真名(最长32字符,一个中文计一个字符 UTF-8编码下)
     * @param idnumber         身份证号(中国的身份证号规则)
     * @param login_device_cnt 可在线设备数
     * @param birthday         生日(UNIX时间戳)
     * @param hometown         家乡
     * @param nation           民族
     * @param profession       工种
     * @param works_cate       工程类别
     * @param working_years    工龄
     * @param online_status    用户在线状态(0: 离线 1: 在线)默认1
     * @param activity
     */
    public void By_UserProfile_update(String uid, String nickname, String sign, int sex, String realname, String idnumber, int login_device_cnt,
                                      String birthday, String hometown, String nation, String profession, String works_cate, int working_years, int online_status, Activity activity) {
        String typeKey = "By_UserProfile_update";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("nickname", nickname);
        map.put("sign", sign);
        map.put("sex", sex);
        if (realname != null) {
            map.put("realname", realname);
        }
        map.put("idnumber", idnumber);
        map.put("login_device_cnt", login_device_cnt);
        map.put("birthday", birthday);
        map.put("hometown", hometown);
        map.put("nation", nation);
        map.put("profession", profession);
        map.put("works_cate", works_cate);
        map.put("working_years", working_years);
        map.put("online_status", online_status);
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "101").buildToString();
        rx.Observable<BaseResponse<LoginInfo>> observable = MyApplication.Companion.getInstance().getMIApi().By_UserAccount_loginByMobile(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<LoginInfo>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_UserProfile_update_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_UserProfile_update_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(LoginInfo.class));
    }


    /**
     * 单图上传
     *
     * @param type
     * @param uid
     * @param md5
     * @param sha1
     * @param ori_name
     * @param file
     * @param activity
     */
    public void avatar(String type, String uid, String md5, String sha1, String ori_name, File file, Activity activity) {
//        File file1 = new File("/storage/emulated/0/Gallery/Pictures/crop/20171223161457.jpg");

        final LoadingDialog progressDialog;
        progressDialog = new LoadingDialog();
        progressDialog.setMsg("正在上传中,请稍后");
        progressDialog.show(activity.getFragmentManager(), activity.getPackageName());
        progressDialog.setCancelable(true);
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("image/jpg"), file);
        MultipartBody.Part photoPart = MultipartBody.Part.createFormData("by_image", file.getName(),
                requestFile);
        rx.Observable<UpdateImageBean> observable = MyApplication.Companion.getInstance().getMIApi().avatar(type, uid, md5, sha1, ori_name, photoPart);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new rx.Observer<UpdateImageBean>() {
            @Override
            public void onCompleted() {
                progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable e) {
                progressDialog.dismiss();
                ResultEntity entity = new ResultEntity(-1, new Object(), e.getMessage(), 0);
                entity.setEventType(avatar_Fail);
                setChangedAndNotifyObservers(entity);
            }

            @Override
            public void onNext(UpdateImageBean updateImageBean) {
                progressDialog.dismiss();
                Gson gson = new Gson();
                UpdateImageBean.UploadImageDataBean bean = null;
                ResultEntity entity = null;
                if (updateImageBean.getData() instanceof ArrayList) {
                    Log.v(Const.Companion.getTAG_DEBUG(), "我是数组" + updateImageBean.getData());
                    entity = new ResultEntity(updateImageBean.getCode(), updateImageBean.getMsg(), updateImageBean.getMsg(), 0);
                    entity.setEventType(avatar_Fail);
                } else {
//                    JSONObject jsonObject = new JSONObject((Map) updateImageBean.getData());
//                    try {

//                        Object listArray = new JSONTokener(json).nextValue();
//                        if (listArray instanceof JSONArray) {
//                            Log.v(Const.Companion.getTAG_DEBUG(), "我是数组" + jsonObject);
//                        } else if (listArray instanceof JSONObject) {
//                            Log.v(Const.Companion.getTAG_DEBUG(), "我是JSONObject" + jsonObject);
//
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
                    String json = gson.toJson(updateImageBean.getData());
                    bean = gson.fromJson(json, UpdateImageBean.UploadImageDataBean.class);
                    Log.v(Const.Companion.getTAG_DEBUG(), "我是其他" + updateImageBean.getData());
                    entity = new ResultEntity(updateImageBean.getCode(), bean, updateImageBean.getMsg(), 0);
                    entity.setEventType(avatar_Success);
                }
                setChangedAndNotifyObservers(entity);
            }
        });
    }

//    1. 类型(借支: 00S002004, 结算: 00S002005, 点工: 00S002003, 包工: 00S002002 )
//    item_type_key
//
//00S002004
//2. 记录的用户id
//            record_uid
//
//1047
//        3. 工人手机号
//            worker_mobile
//
//13484379290
//        4. 工人姓名(1 - 20)
//    worker_name
//
//            test
//5. 工头手机号
//            overseer_mobile
//
//13484379291
//        6. 工头姓名(1 - 20)
//    overseer_name
//
//            test
//7. 项目名称
//            project_title
//
//    xx小区一期项目工程
//8. 子项名称
//            sub_title
//
//    粉刷
//9. 备注(最大256个字符,一个中文计一个字符)
//    note
//
//            有意见
//10. 日期字符串格式(20171227)
//    daytime
//
//20180101
//        11. 金额(单位: 分)
//    money
//
//0
//        12. 单价(包工类型才需要传 单位: 分)
//    price
//
//500
//        13. 数量(包工类型才需要)
//    count
//
//10
//        14. 时薪(单位: 元)[点工]
//    hourly_wage
//
//500
//        15. 上班时间(小时)[点工]
//    normal_worktime
//
//8
//        16. 加班时长(小时)[点工]
//    over_worktime

    public void By_JgbRecord_add(String item_type_key,
                                 String record_uid,
                                 String worker_mobile,
                                 String worker_name,
                                 String overseer_mobile,
                                 String overseer_name,
                                 String project_title,
                                 String sub_title,
                                 String note,
                                 String daytime,
                                 double money,
                                 double price,
                                 int count,
                                 double hourly_wage,
                                 String normal_worktime,
                                 String over_worktime,
                                 Activity activity) {
        String typeKey = "By_JgbRecord_add";
        HashMap<String, Object> map = new HashMap<>();
        if (item_type_key != null) {
            map.put("item_type_key", item_type_key);
        }
        if (record_uid != null) {
            map.put("record_uid", record_uid);
        }
        if (worker_mobile != null) {
            map.put("worker_mobile", worker_mobile);
        }
        if (worker_name != null) {
            map.put("worker_name", worker_name);
        }
        if (overseer_mobile != null) {
            map.put("overseer_mobile", overseer_mobile);
        }
        if (overseer_name != null) {
            map.put("overseer_name", overseer_name);
        }
        if (project_title != null) {
            map.put("project_title", project_title);
        }
        if (sub_title != null) {
            map.put("sub_title", sub_title);
        }
        if (note != null) {
            map.put("note", note);
        }
        if (daytime != null) {
            map.put("daytime", daytime);
        }
        if (money >= 0) {
            map.put("money", money);
        }
        if (price >= 0) {
            map.put("price", price);
        }
        if (count >= 0) {
            map.put("count", count);
        }
        if (hourly_wage >= 0) {
            map.put("hourly_wage", hourly_wage);
        }
        if (normal_worktime != null) {
            map.put("normal_worktime", normal_worktime);
        }
        if (over_worktime != null) {
            map.put("over_worktime", over_worktime);
        }
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_add(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_add_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_add_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(String.class));
    }

    /**
     * 1. 更新人ID
     * operator_uid
     * <p>
     * 20
     * 2. 记录id
     * id
     * <p>
     * 20
     * 3. 类型(借支: 00S002004, 结算: 00S002005, 点工: 00S002003, 包工: 00S002002 )
     * item_type_key
     * <p>
     * 00S002004
     * 4. 工人手机号
     * worker_mobile
     * <p>
     * 13484379290
     * 5. 工人姓名(1 - 20)
     * worker_name
     * <p>
     * test
     * 6. 工头手机号
     * overseer_mobile
     * <p>
     * 13484379291
     * 7. 工头姓名(1 - 20)
     * overseer_name
     * <p>
     * test
     * 8. 项目名称
     * project_title
     * <p>
     * xx小区一期项目工程
     * 9. 子项名称
     * sub_title
     * <p>
     * 粉刷
     * 10. 备注(最大256个字符,一个中文计一个字符)
     * note
     * <p>
     * 有意见
     * 11. 日期字符串格式(20171227)
     * daytime
     * <p>
     * 20180101
     * 12. 金额(单位: 分)
     * money
     * <p>
     * 0
     * 13. 单价(包工类型才需要传 单位: 分)
     * price
     * <p>
     * 500
     * 14. 数量(包工类型才需要)
     * count
     * <p>
     * 10
     * 15. 时薪(单位: 元)[点工]
     * hourly_wage
     * <p>
     * 500
     * 16. 上班时间(小时)[点工]
     * normal_worktime
     * <p>
     * 8
     * 17. 加班时长(小时)[点工]
     * over_worktime
     */
    public void By_JgbRecord_update(String operator_uid,
                                    int id,
                                    String item_type_key,
                                    String worker_mobile,
                                    String worker_name,
                                    String overseer_mobile,
                                    String overseer_name,
                                    String project_title,
                                    String sub_title,
                                    String note,
                                    String daytime,
                                    double money,
                                    double price,
                                    int count,
                                    double hourly_wage,
                                    String normal_worktime,
                                    String over_worktime,
                                    Activity activity) {
        String typeKey = "By_JgbRecord_update";
        HashMap<String, Object> map = new HashMap<>();
        if (operator_uid != null) {
            map.put("record_uid", operator_uid);
        }
        map.put("id", id);
        if (item_type_key != null) {
            map.put("item_type_key", item_type_key);
        }

        if (worker_mobile != null) {
            map.put("worker_mobile", worker_mobile);
        }
        if (worker_name != null) {
            map.put("worker_name", worker_name);
        }
        if (overseer_mobile != null) {
            map.put("overseer_mobile", overseer_mobile);
        }
        if (overseer_name != null) {
            map.put("overseer_name", overseer_name);
        }
        if (project_title != null) {
            map.put("project_title", project_title);
        }
        if (sub_title != null) {
            map.put("sub_title", sub_title);
        }
        if (note != null) {
            map.put("note", note);
        }
        if (daytime != null) {
            map.put("daytime", daytime);
        }
        if (money >= 0) {
            map.put("money", money);
        }
        if (price >= 0) {
            map.put("price", price);
        }
        if (count >= 0) {
            map.put("count", count);
        }
        if (hourly_wage >= 0) {
            map.put("hourly_wage", hourly_wage);
        }
        if (normal_worktime != null) {
            map.put("normal_worktime", normal_worktime);
        }
        if (over_worktime != null) {
            map.put("over_worktime", over_worktime);
        }
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_update(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_update_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_update_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(String.class));

    }


    public void By_JgbRecord_overseerIndex(String mobile, Activity activity) {
        String typeKey = "By_JgbRecord_overseerIndex";
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        Type type = new TypeToken<ArrayList<MonthAccountBean>>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<ArrayList<MonthAccountBean>>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_overseerIndex(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<ArrayList<MonthAccountBean>>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_overseerIndex_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_overseerIndex_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_workerIndex(String mobile, Activity activity) {
        String typeKey = "By_JgbRecord_workerIndex";
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        Type type = new TypeToken<ArrayList<MonthAccountBean>>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<ArrayList<MonthAccountBean>>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_workerIndex(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<ArrayList<MonthAccountBean>>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_workerIndex_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_workerIndex_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

//    1. 工头姓名(注意是工头姓名)
//    name
//
//    工头姓名(注意是工头姓名)
//2. 项目名称
//            project_title
//
//    项目名称
//3. 工人手机号
//            mobile
//
//15736243109
//        4. 月份格式：20171201
//    daytime
//
//20180111
//        5. 页下标
//            page_index
//
//1
//        6. 页码
//            page_size

    public void By_JgbRecord_queryWorker(String name, String project_title, String mobile, String daytime, int page_index, int page_size, Activity activity) {
        String typeKey = "By_JgbRecord_queryWorker";
        HashMap<String, Object> map = new HashMap<>();
        if (name != null) {
            map.put("name", name);
        }
        if (project_title != null) {
            map.put("project_title", project_title);
        }
        if (mobile != null) {
            map.put("mobile", mobile);
        }
        if (daytime != null) {
            map.put("daytime", daytime);
        }
        map.put("page_index", (page_index));
        map.put("page_size", page_size);
        Type type = new TypeToken<JiYiBiBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<JiYiBiBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_queryWorker(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<JiYiBiBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryWorker_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryWorker_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_queryOverseer(String name, String project_title, String mobile, String daytime, int page_index, int page_size, Activity activity) {
        String typeKey = "By_JgbRecord_queryOverseer";
        HashMap<String, Object> map = new HashMap<>();
        if (name != null) {
            map.put("name", name);
        }
        if (project_title != null) {
            map.put("project_title", project_title);
        }
        if (mobile != null) {
            map.put("mobile", mobile);
        }
        if (daytime != null) {
            map.put("daytime", daytime);
        }
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        Type type = new TypeToken<JiYiBiBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<JiYiBiBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_queryOverseer(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<JiYiBiBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryOverseer_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryOverseer_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_queryAuditRecordForOverseer(String uid, String mobile, int page_index, int page_size, Activity activity) {
        String typeKey = "By_JgbRecord_queryAuditRecordForOverseer";
        HashMap<String, Object> map = new HashMap<>();
        if (uid != null) {
            map.put("uid", uid);
        }
        if (mobile != null) {
            map.put("mobile", mobile);
        }
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        Type type = new TypeToken<JiYiBiBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<JiYiBiBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_queryAuditRecordForOverseer(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<JiYiBiBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAuditRecordForOverseer_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAuditRecordForOverseer_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_queryAuditRecordForWorker(String uid, String mobile, int page_index, int page_size, Activity activity) {
        String typeKey = "By_JgbRecord_queryAuditRecordForWorker";
        HashMap<String, Object> map = new HashMap<>();
        if (uid != null) {
            map.put("uid", uid);
        }
        if (mobile != null) {
            map.put("mobile", mobile);
        }
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        Type type = new TypeToken<JiYiBiBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<JiYiBiBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_queryAuditRecordForWorker(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<JiYiBiBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAuditRecordForWorker_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAuditRecordForWorker_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_audit(int id, int operator_uid, int audit_status, Activity activity) {
        String typeKey = "By_JgbRecord_audit";
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("operator_uid", operator_uid);
        map.put("audit_status", audit_status);
        Type type = new TypeToken<String>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_audit(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_audit_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_audit_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbFinanceBill_worker(String mobile, String daytime, Activity activity) {
        String typeKey = "By_JgbFinanceBill_worker";
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
//        map.put("daytime", daytime);
        Type type = new TypeToken<FinanceBillBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "101").buildToString();
        rx.Observable<BaseResponse<FinanceBillBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbFinanceBill_worker(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<FinanceBillBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbFinanceBill_worker_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbFinanceBill_worker_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbFinanceBill_overseer(String mobile, String daytime, Activity activity) {
        String typeKey = "By_JgbFinanceBill_overseer";
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("daytime", daytime);
        Type type = new TypeToken<FinanceBillBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "101").buildToString();
        rx.Observable<BaseResponse<FinanceBillBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbFinanceBill_overseer(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<FinanceBillBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbFinanceBill_overseer_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbFinanceBill_overseer_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_Message_query(String uid, int msg_type, int page_index, int page_size, Activity activity) {
        String typeKey = "By_Message_query";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        if (msg_type >= 0) {
            map.put("msg_type", msg_type);
        }
        map.put("page_index", page_index);
        map.put("page_size", page_size);
        Type type = new TypeToken<ArrayList<NotifyMessageBean>>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<ArrayList<NotifyMessageBean>>> observable = MyApplication.Companion.getInstance().getMIApi().By_Message_query(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<ArrayList<NotifyMessageBean>>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_query_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_query_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_queryAllWorkerName(String uid, Activity activity) {
        String typeKey = "By_JgbRecord_queryAllWorkerName";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        Type type = new TypeToken<ArrayList<WorkerBean>>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<ArrayList<WorkerBean>>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_queryAllWorkerName(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<ArrayList<WorkerBean>>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAllWorkerName_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_queryAllWorkerName_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_Message_info(String uid, String id, Activity activity) {
        String typeKey = "By_Message_info";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        Type type = new TypeToken<NotifyMessageBean>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<NotifyMessageBean>> observable = MyApplication.Companion.getInstance().getMIApi().By_Message_info(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<NotifyMessageBean>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_info_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_info_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_JgbRecord_info(String id, Activity activity) {
        String typeKey = "By_JgbRecord_info";
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        Type type = new TypeToken<JiYiBiBean.JiYiBiEntity>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<JiYiBiBean.JiYiBiEntity>> observable = MyApplication.Companion.getInstance().getMIApi().By_JgbRecord_info(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<JiYiBiBean.JiYiBiEntity>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_info_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_JgbRecord_info_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_Message_unreadCount(String uid, Activity activity) {
        String typeKey = "By_Message_unreadCount";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        Type type = new TypeToken<Integer>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<Integer>> observable = MyApplication.Companion.getInstance().getMIApi().By_Message_unreadCount(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<Integer>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_unreadCount_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_unreadCount_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_Message_del(@NotNull String uid, @Nullable String id, Activity activity) {
        String typeKey = "By_Message_del";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        Type type = new TypeToken<String>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_Message_del(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_del_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Message_del_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void By_Suggest_add(@NotNull String uid, @Nullable String suggest_content, Activity activity) {
        String typeKey = "By_Suggest_add";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("suggest_content", suggest_content);
        map.put("extra", "");
        Type type = new TypeToken<String>() {
        }.getType();
        String itboyeData = new BuilMapUtils.buildParams(typeKey, map)
                .addParams(Const.Companion.getTYPE(), typeKey)
                .addParams(Const.Companion.getAPI_VER(), "100").buildToString();
        rx.Observable<BaseResponse<String>> observable = MyApplication.Companion.getInstance().getMIApi().By_Suggest_add(itboyeData);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseResponse<String>>(activity, new ResponseListener() {
            @Override
            public void success(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Suggest_add_Success);
                setChangedAndNotifyObservers(resultEntity);
            }

            @Override
            public void fail(ResultEntity resultEntity) {
                resultEntity.setEventType(By_Suggest_add_Fail);
                setChangedAndNotifyObservers(resultEntity);
            }
        }).setExpectReturnType(type));
    }

    public void setChangedAndNotifyObservers(Object ob) {
        UserPrestener.this.setChanged();
        UserPrestener.this.notifyObservers(ob);
    }


}
